package algorithm.sort.selectSort;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] arr = {-2, -1, -4, 6, 4, 7, 0, 13, 15, 17};
        selectSort(arr);
        prin(arr);

    }

    public static void selectSort(int[] arr) {
        int min;
        int minIndex;
        for (int i = 0; i < arr.length - 1; i++) {
            min = arr[i];
            minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                //找出min和minIndex
                if (arr[j] < min) {
                    min = arr[j];
                    minIndex = j;
                }
            }
            //如果找到更小的就交换
            if (min < arr[i]) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }

    public static void prin(int[] arr) {
        System.out.println("选择排序结果：");
        System.out.println(Arrays.toString(arr));
    }
}
